/**     
 * @Title: DemoHandler.java   
 * @Package me.xxy.mq.demo.handler   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年5月30日 下午5:39:10   
 * @version V1.0     
 */
package me.xxy.mq.demo.handler;

import me.xxy.mq.core.AbstractMessageHandler;
import me.xxy.mq.demo.MessageType;
import me.xxy.mq.demo.entity.DemoMessage;
import me.xxy.mq.demo.servlet.ConsoleWebSocket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * @ClassName: DemoHandler
 * @Description: TODO
 * @author weiwei
 * @date 2018年5月30日 下午5:39:10
 * 
 */
@Service
public class DemoHandler extends AbstractMessageHandler<DemoMessage> {

	private final static Log LOG = LogFactory.getLog(DemoHandler.class);

	public DemoHandler() {
		// 说明该handler监控的消息类型
		super(MessageType.DEMO_MESSAGE, DemoMessage.class, 10);
	}

	/**
	 * 监听到消息后处理方法
	 */
	@Override
	public void handle(DemoMessage message) {
		ConsoleWebSocket.socket.sendMessage("正在处理消息：" + message.getValue());
		System.out.println("正在处理消息：" + message.getValue());
		// 模拟处理失败
		if ("error".equals(message.getValue())) {
			ConsoleWebSocket.socket.sendMessage("处理失败");
			throw new RuntimeException("处理失败");
		}
		ConsoleWebSocket.socket.sendMessage("处理成功，消息：" + message.getValue());
	}

	@Override
	public void handleFailed(DemoMessage message) {
		StringBuilder sb = new StringBuilder();
		sb.append("msg:[").append(message).append("], 超过失败次数，停止重试。");
		LOG.warn(sb.toString());
		ConsoleWebSocket.socket.sendMessage(sb.toString());
	}
}
